#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
using namespace std;

//const char *err_string[] = {
//    "success",
//    "error"
//};
//
//int add_to_top(int top)
//{
//    printf("enter add_to_top\n");
//    int sum = 0;
//    for(int i = 0; i <= top; i++)
//    {
//        sum += i;
//    }
//    //exit(123);
//    _exit(222);
//    printf("out add_to_top\n");
//    return sum;
//}


int main(){
  pid_t id = fork();
  if(id == 0){
    //子进程
    int cnt = 5;
    while(cnt){
      cout << "child "<<"pid:"<<getpid()<<" ppid:"<<getppid()<< " 剩余时间:" <<cnt-- << endl;
      sleep(1);
    }
    exit(0);
  }
  //父进程
  //pid_t ret = wait(NULL);
  
  //父进程开始时睡眠10s，意味着当子进程结束后其退出码还有5s没被父进程获取，此时子进程为僵尸状态
  sleep(10);
  int status = 0;
  pid_t ret = waitpid(id,&status,0);
  cout <<"parent "<<"pid:"<<getpid()<<" ppid:"<<getppid()<<" ret:"<<ret<<" status:"<<status<<" 退出状态:"<<((status<<8)&0xFF)<<" 终止信号:"<<(status&0x7F)<<endl;
  return 0;
}
